﻿class SpringBalls extends MovieClip {
	var stageWidth:Number;
	var stageHeight:Number;
	var ballCnt:Number = 10;
	var balls:Array;
	//彈性係數
	var spring:Number = .2;
	//摩擦係數
	var friction:Number = .95;
	//地心引力係數，乘上物體質量後，才是真正影響 y 的力量
	var gravity:Number = 0.8;
	function SpringBalls() {
		init();
	}
	function init():Void {
		stageWidth = Stage.width;
		stageHeight = Stage.height;
		balls = new Array();
		for (var i = 0; i<ballCnt; i++) {
			var ball_mc:MovieClip = this.createEmptyMovieClip("ball"+i+"_mc", 100+i);
			var size:Number = 10+Math.random()*10;
			var x0:Number = (i+1)*(stageWidth/(ballCnt+1));
			var y0:Number = stageHeight/2;
			var ball:Ball = new Ball(ball_mc, size, x0, y0);
			ball.x = x0;
			ball.y = y0+(stageHeight*.5*.8)*Math.random();
			balls.push(ball);
		}
	}
	function onEnterFrame():Void {
		clear();
		lineStyle(0, 0x000000, 100);
		for (var i:Number = 0; i<balls.length; i++) {
			var ball:Ball = balls[i];
			var ax:Number = (ball.x0-ball.x)*spring;
			var ay:Number = (ball.y0-ball.y)*spring;
			ball.vx += ax;
			ball.vy += ay;
			ball.vy += (ball.size*gravity);
			ball.vx *= friction;
			ball.vy *= friction;
			ball.x += ball.vx;
			ball.y += ball.vy;
			moveTo(ball.x0, ball.y0);
			lineTo(ball.x, ball.y);
		}
	}
}
